<?php
/**
*
*	API设置 @zairwolf
*/
if(!defined('IN_ACP')) exit('Access Denied');

$id=_get('id');
//修改charset
if($action == 'charset') {
	if(!$id || !ris_int($id)) b('非法操作');

	db_q('UPDATE rd8_api SET `charset` = `charset` % 3 + 1 WHERE id = '.$id);
	j("?ptype=$ptype&pfile=$pfile", '修改成功');
}
//修改gate
if($action == 'gate') {
	if(!$id || !ris_int($id)) b('非法操作');

	db_q('UPDATE rd8_api SET gate = gate % 2 + 1 WHERE id = '.$id);
	j("?ptype=$ptype&pfile=$pfile", '修改成功');
}
//锁定
if($action == 'lock') {
	if(!$id || !ris_int($id)) b('非法操作');

	db_q('UPDATE rd8_api SET active = (active + 1) % 2 WHERE id = '.$id);
	j("?ptype=$ptype&pfile=$pfile", '修改成功');
}
//编辑
if($action == 'edit') {
	if(!$id || !ris_int($id)) b('非法操作');

	$tpl = new T($ptype.'_'.$pfile.'_'.$action);
	$tpl->assign(db_r("select * from rd8_api where id='$id'"));
	$tpl->output();
}
//更新
if($action == 'update') {
	$id=_post('id');
	if(!$id || !ris_int($id)) b('非法操作');
	$address=_post('address');
	$code=_post('code');

	db_q("update rd8_api set address='$address',code='$code' where id='$id'");
	j("?ptype=$ptype&pfile=$pfile", '修改成功');
}
//删除
if($action == 'delete') {
	if(!$id || !ris_int($id)) b('非法操作');

	db_q("delete from rd8_api where id='$id'");
	j("?ptype=$ptype&pfile=$pfile", '删除成功');
}

//api列表中的更新
if($action=='addupdate') {
	$api=_post('api');
	$address=_post('address');
	if(!empty($api)) {
		if($api != 'general' && $api != 'ucenter') $api = 'none';

		db_q("update rd8_system set info='$api' where config='SYSTEM' and title='api'");

		_config_update('system');
	}

	if(!empty($address)) {
		if(substr($address, 0, 7) != 'http://' && substr($address, 0, 8) != 'https://') b('地址需加"http://"');
		if(substr($address, -1) == '/') b('末尾不加"/"');

		db_i("insert into rd8_api set address='$address'");
	}
	j("?ptype=$ptype&pfile=$pfile", '修改成功');
}

//默认显示
if(!$action){
	$tpl = new T($ptype.'_'.$pfile);
	$tpl->assign(array(
		'api_none_c'    => $_SYSTEM['SYSTEM']['api'] == 'none'    ? ' checked="checked"' : '',
		'api_general_c' => $_SYSTEM['SYSTEM']['api'] == 'general' ? ' checked="checked"' : '',
		'api_ucenter_c' => $_SYSTEM['SYSTEM']['api'] == 'ucenter' ? ' checked="checked"' : '',
		'api_none_d'    => $_SYSTEM['SYSTEM']['api'] != 'none'    ? ' style="display: none;"' : '',
		'api_general_d' => $_SYSTEM['SYSTEM']['api'] != 'general' ? ' style="display: none;"' : '',
		'api_ucenter_d' => $_SYSTEM['SYSTEM']['api'] != 'ucenter' ? ' style="display: none;"' : '',

		'apis' => db_rows("select * from rd8_api"),
	));

	$tpl->output();	
}
